package cn.com.dhcc.turbo.web.controller.monitor;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.dhcc.app.core.base.BaseController;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.handler.modulehandler.valuebean.BetweenValue;
import cn.com.dhcc.app.core.handler.pagerhandler.Pager;
import cn.com.dhcc.app.core.handler.pagerhandler.PagerData;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.core.util.DTUtil;
import cn.com.dhcc.app.core.util.StrUtil;
import cn.com.dhcc.app.pub.core.annotation.TurboMenu;
import cn.com.dhcc.app.pub.core.annotation.TurboMenu.LEAF_MENU;
import cn.com.dhcc.turbo.entity.exch.CarvedDiscsData;
import cn.com.dhcc.turbo.entity.exch.CarvedDiscsHistory;
import cn.com.dhcc.turbo.service.exch.cmdata.CarvedDiscsDataService;
import cn.com.dhcc.turbo.service.exch.cmdata.CarvedDiscsHistoryService;
import cn.com.dhcc.turbo.service.exch.cmdata.SendCmDataService;
import cn.com.dhcc.turbo.service.registry.BaseInfoService;


/**
 * 刻盘管理Controller
 * 
 * @日期 2014-10-14 下午2:34:28
 * @作者  高强
 */
@Controller
@RequestMapping("/monitor")
@TurboMenu(belongToMenu=LEAF_MENU.CARVED_DISCS_MANAGE)
public class CarvedDiscsHistoryController extends BaseController {
	@SuppressWarnings("deprecation")
	private static Logger logger = Log.getExistLogger(LOGTYPE.SAI);

	@Autowired
	private CarvedDiscsDataService carvedDiscsDataService;
	@Autowired
	private CarvedDiscsHistoryService carvedDiscsHistoryService;
	@Autowired
	private SendCmDataService cmDataService;
	
	@Autowired
	private BaseInfoService baseInfoService;
	
	@RequestMapping(value = "/carved_discs_history_list")
	public String list(
			Model model, 
			Pager pager, 
			String sendBusicode, 
			String batchId,
			String startCreated, 
			String endCreated, 
			String orderName, 
			String order) {
		try {
			WhereCondition searchData = new WhereCondition();
			searchData.where().notEq("id", "0");
			
			if (StrUtil.isNotBlank(sendBusicode)) {
				searchData.and().like("send_busicode", sendBusicode);
			}
			
			if (StrUtil.isNotBlank(batchId)) {
				searchData.and().like("batch_id", batchId);
			}
			
			//这里对发生中的状态进行了处理，把本地发送和远程发送都看做为发送中
			/*if (StrUtil.isNotBlank(sendStatus)) {
				if (AppConst.SendStatus.LOCAL_SENDING.value().equals(sendStatus)) {
					searchData.and().leftBracket()
								.eq("send_status", sendStatus)
								.or()
								.eq("send_status", AppConst.SendStatus.REMOTE_SENDING.value())
								.rightBracket();
				} else {
					searchData.and().eq("send_status", sendStatus);
				}
			}*/
			//时间范围从那天到那天
			if (StrUtil.isNotBlank(startCreated) && StrUtil.isNotBlank(endCreated)) {
				searchData.and().between("created", new BetweenValue(DTUtil.toDateTime(startCreated) , DTUtil.toDateTime(endCreated)));
			}

			//时间范围，大于等于某天
			if (StrUtil.isNotBlank(startCreated) && StrUtil.isBlank(endCreated)) {	
				searchData.and().between("created", new BetweenValue(DTUtil.toDateTime(startCreated), null, true, false));
			}

			//时间范围，小于等于某天
			if (StrUtil.isBlank(startCreated) && StrUtil.isNotBlank(endCreated)) {	
				searchData.and().between("created", new BetweenValue(null, DTUtil.toDateTime(endCreated), false, true));
			}
			
			if (StrUtil.isNotBlank(orderName)) {
				searchData.orderBy(orderName + " " + order);
			} else {
				searchData.orderBy("created  desc");
			}
			PagerData<CarvedDiscsHistory> datas = carvedDiscsHistoryService.getPagerList(pager, searchData);
			List<CarvedDiscsHistory> dataList = datas.getDatas();
			model.addAttribute(DATA_KEY, dataList);
			setTotoal(model, datas.getTotal());
			return "monitor/carved_discs_history_list";
		} catch (Exception e) {
			logger.error("操作失败", e);
			return toAlertPage(model, e.getMessage());
		}
	}

	@RequestMapping(value = "/carved_discs_history_get/{id}")
	public String get(@PathVariable String id,Model model,Pager pager){
			try {
				CarvedDiscsHistory vo = carvedDiscsHistoryService.get(id);
				WhereCondition searchData = new WhereCondition();
				searchData.where().in("id", vo.getCarvedDiscsIds().split(","));
				PagerData<CarvedDiscsData> datas = carvedDiscsDataService.getPagerList(pager, searchData);
			    List<CarvedDiscsData> dataList = datas.getDatas();
			    model.addAttribute(DATA_KEY, dataList);
			    model.addAttribute(VO_KEY, vo);
			    setTotoal(model, datas.getTotal());
				return "monitor/carved_discs_history_get";
			} catch (Exception e) {
				logger.error("操作失败", e);
				return toAlertPage(model, e.getMessage());
			} 
	}
	
	
}
